package com.ceba.test.modules.signvisual.logicService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONArray;
import com.ceba.base.utils.IDSFileUtils;
import com.ceba.base.utils.PDFUtils;
import com.ceba.base.web.response.IResult;
import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.entity.SignPrintPDF;
import com.ceba.test.modules.admin.entity.GStamp;
import com.ceba.test.modules.sign.logicService.SignLogicService;
import com.ceba.test.modules.signvisual.logicService.abstracts.SignVisualAbstract;
import com.ceba.test.modules.statistic.entity.ReportSign;

import abc.data.Sql;
import abc.data.SqlSessionManager;
import configs.context.SpringBeans;

public class SignVisualLogicAbstractService extends SignVisualAbstract {
	private SignLogicService signLogicService = SpringBeans.getSignLogicService("signLogicService");
	/**
	 * 盖章
	 * @param signPrintPDFList 用户章 信息
	 * @param pdfPath 盖章报告
	 * @return 盖章后生成的报告路径 --临时路径
	 */
	@Override
	public String seal(List<SignPrintPDF> signPrintPDFList, String pdfPath,String outPdfPath)  throws Exception {
		if(CollectionUtils.isEmpty(signPrintPDFList)){return null;}
		String outPath= LogicPathUtil.getPhysicalTempFolder(StorageFileCacheManage.getSystemUseAddressId())+"outSeal"+File.separator;
		IDSFileUtils.createDir(outPath);
		signPrintPDFToIdG_Stamp(signPrintPDFList);
		List<GStamp> g_StampList=signPrintPDFToG_Stamp(signPrintPDFList);
		outPath=outPath+new File(pdfPath).getName();
		boolean bool=PDFUtils.insertImagesByMatchNameOrXY(pdfPath,outPath, new JSONArray(g_StampList));
		if(!bool){
			return null;
		}else{
			for(int index=0;index<g_StampList.size();index++){
				GStamp g_Stamp=g_StampList.get(index);
				if(StringUtils.isNotBlank(g_Stamp.getId())){
					
					Sql sql = new Sql("update g_stamp set img_width='"+NumberUtils.toFloat(g_Stamp.getImgWidth())+"',img_height='"+NumberUtils.toFloat(g_Stamp.getImgHeight())+"',stamp_page='"+g_Stamp.getStampPage()+"',stamp_x="+toOneFloat(g_Stamp.getStamp_x())+",stamp_y="+toOneFloat(g_Stamp.getStamp_y())+" where id='"+g_Stamp.getId()+"'");
					//SqlSessionManager.getDataTable(sql);
					SqlSessionManager.execute(sql);
				}
			}
			
		}
		return outPath;
	}
	
	/**
	 * 签名
	 * @param signPrintPDFList 用户章 信息
	 * @param pdfPath 签名报告
	 * @return 签名后生成的报告路径 --临时路径
	 */
	@Override
	public String autograph(List<SignPrintPDF> signPrintPDFList, String pdfPath,String outPdfPath,ReportSign parm)  throws Exception {
		String outPath=LogicPathUtil.getPhysicalTempFolder(StorageFileCacheManage.getSystemUseAddressId())+"outAutograph"+File.separator;
		IDSFileUtils.createDir(outPath);
		signPrintPDFToIdG_Stamp(signPrintPDFList);
		List<GStamp> g_StampList=signPrintPDFToG_Stamp(signPrintPDFList);
		IResult result = new IResult(true);
		List<GStamp> stamps = new ArrayList<GStamp>();
		stamps.add(g_StampList.get(0));
		signLogicService.signPdf(pdfPath,outPath,stamps,result,parm);
		if(result.getResult()){
			pdfPath = result.getString("filePath");
			return outPath;
		}
		return null;
	}
	
	private int toOneFloat(String count){
		float price=NumberUtils.toFloat(count);
		return (int) Math.floor(price);
	}


}
